package com.ad.pet.app.collect.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.app.collect.CollecContract;
import com.ad.pet.app.collect.CollectActivity;
import com.ad.pet.app.collect.CollectPresenter;
import com.ad.pet.app.collect.adapter.CollectGoodAdapter;
import com.ad.pet.common.structure.events.Events;
import com.ad.pet.common.structure.events.RxBus;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.fragment.BaseFragment;
import com.ad.pet.models.remote.account.order.OrderType;
import com.ad.pet.ui.SimpleDividerDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * Created by SEELE on 2018/7/2.
 */

public class GoodCollectFragment extends BaseFragment implements CollecContract.View{

    @BindView(R.id.recycler_good)
    RecyclerView mRecyclerGood;


    @Inject
    CollectPresenter mCollectPresenter;
    public CollectGoodAdapter mMCollectGoodAdapter;

    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mSwipeRefresh;

    @Inject
    RxDisposable mRxDisposable;

    int page=1;
    @Override
    protected void initData() {

        mCollectPresenter.onCreate(this);
        mCollectPresenter.getCollectGoods("1");

    }

    @Override
    protected void initEvents() {

        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mMCollectGoodAdapter.setEnableLoadMore(false);
            mCollectPresenter.getCollectGoods("1");

            }
        });
//        mMCollectGoodAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//
//                mCollectPresenter.getCollectGoods("1");
//
//
//            }
//        }, mRecyclerGood);


        mMCollectGoodAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Navigation.goGoodDetail(getActivity(), MainActivity.REQUEST_GO_MALL,mMCollectGoodAdapter.getData().get(position).getId());
            }
        });


        mRxDisposable.add(RxBus.getDefault().toObservable(Events.Collection.class).subscribe(new Action1<Events.Collection>() {
            @Override
            public void call(Events.Collection collection) {
                page=1;
                mCollectPresenter.getCollectGoods("1");
            }
        }));
    }



    @Override
    protected void initViews() {


        mMCollectGoodAdapter = new CollectGoodAdapter(null);
        mRecyclerGood.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerGood.addItemDecoration(new SimpleDividerDecoration(getActivity(),2,R.color.graycf));
        mRecyclerGood.setAdapter(mMCollectGoodAdapter);

        mMCollectGoodAdapter.bindToRecyclerView(mRecyclerGood);
//        mMCollectGoodAdapter.setEmptyView(R.layout.layout_empty_address);
//        ((TextView)mMCollectGoodAdapter.getEmptyView().findViewById(R.id.tv_title)).setText("还没有相关收藏哦~");

    }

    @Override
    public void initInjector() {
        DaggerMainActivityCompent.builder().applicationComponent(((CollectActivity)getActivity()).getApplicationComponent())

                .activityModule(((CollectActivity)getActivity()).getActivityModule()).build().inject(this);
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_good_collection;
    }

    @Override
    protected BasePresenter getPresenter() {
        return mCollectPresenter;
    }



    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void getCollectGoodsSuccess(List<OrderType> allGood) {
        mSwipeRefresh.setRefreshing(false);
//        if (page == 1) {
            mMCollectGoodAdapter.setNewData(allGood);
//        } else {
//            mMCollectGoodAdapter.addData(allGood);
//            mMCollectGoodAdapter.loadMoreComplete();
//        }
//        if (allGood.size() <10) {//CommonConstant.PAGE_COUNT
//            //暂无更多数据
//            mMCollectGoodAdapter.loadMoreEnd();
//        }
        if (allGood.size() == 0 ) {
                    mMCollectGoodAdapter.setEmptyView(R.layout.layout_empty_address);
        ((TextView)mMCollectGoodAdapter.getEmptyView().findViewById(R.id.tv_title)).setText("还没有相关收藏哦~");
        }
    }






    @Override
    public void onResume() {
        super.onResume();

    }
}
